package com.neutech.filter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(urlPatterns = "/*")
public class CommonSettingFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        //设置请求对象编码集
        request.setCharacterEncoding("utf-8");
        //设置跨域
        response.setHeader("Access-Control-Allow-Origin","http://localhost:8888");
        response.setHeader("Access-Control-Allow-Headers","Content-Type,Content-Length,Authorization,Accept,X-Requested-With");
        response.setHeader("Access-Control-Allow-Credentials","true");
        //设置响应格式
        servletResponse.setContentType("application/json;charset=utf-8");
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
